﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Account;
using ManagementApi.Models.Constructor;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Constructor;
using Modules.System.Security;
using Modules.System.User;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Controllers.ConstructorPannel
{
    [Route("/api/v1/constructor/Constructor")]
    [ApiController]
    [Authorize(Roles = "Constructor")]
    public class ConstructorController : ControllerBase
    {
        private readonly ConstructorService constructorService;
        private readonly ConstructorApplicationContext applicationContext;
        private readonly SecurityService securityService;
        private readonly IMapper mapper;

        public ConstructorController(ConstructorService constructorService, ConstructorApplicationContext applicationContext, SecurityService securityService, IMapper mapper)
        {
            this.constructorService = constructorService;
            this.applicationContext = applicationContext;
            this.securityService = securityService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取建设单位信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("getConstructorInfo")]
        public ApiResult<ConstructorDto> GetConstructorInfo()
        {
            ConstructorInfo? constructorInfo = constructorService.GetConstructorInfo(applicationContext.CurrentUser?.Id ?? 0);
            if (constructorInfo == null)
                return ApiResult<ConstructorDto>.Failure("建设单位信息不存在");
            return ApiResult<ConstructorDto>.Success(constructorInfo.AsDto());
        }

        /// <summary>
        /// 修改建设单位信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("updateConstructor")]
        public ApiResult UpdateConstructor(ConstructorCreateForm form)
        {
            ConstructorInfo? constructorInfo = constructorService.GetConstructorInfo(applicationContext.CurrentUser?.Id ?? 0);
            if (constructorInfo == null)
                return ApiResult.Failure("建设单位信息不存在");
            mapper.Map(form, constructorInfo);
            constructorService.Update(constructorInfo);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passWord"></param>
        /// <returns></returns>
        [HttpPut("updateConstructorPassWord")]
        public ApiResult UpdateConstructorPassWord([Required(ErrorMessage = "密码不能为空")] string passWord)
        {
            ConstructorInfo? constructorInfo = constructorService.GetConstructorInfo(applicationContext.CurrentUser?.Id ?? 0);
            if (constructorInfo == null)
                return ApiResult.Failure("建设单位信息不存在");
            SysSecurity? sysSecurity = securityService.GetSysSecurity(constructorInfo.SecurityId);
            if (sysSecurity != null)
            {
                sysSecurity.Password = SecurityPasswordHelper.EncodePassword(passWord, UserPasswordFormat.SM4);
                securityService.Update(sysSecurity);
            }
            return ApiResult.Success("修改成功");
        }
    }
}